<template>
	<view class="serachResult" v-if="isResultShow">
		<view class="tab">
			<view class="tablist tabVideo" :class="silderIndex === 0 ? 'active' : '' " @click="getChnage(0)">视频</view>
			<view class="tablist tabUser" :class="silderIndex === 1 ? 'active' : '' " @click="getChnage(1)">用户</view>
		</view>
		<swiper class="swiper" :indicator-dots="false"  :current="silderIndex"  :duration="500" @change="changeIndicatorDots">
			<swiper-item>
				<scroll-view class="sView" :scroll-y="true" @scrolltolower="flash0"    v-if="videoList.length > 0">
				<view class="videoSwiper">
					<view
						class="list"
						v-for="(item, index) in videoList" 
						:key="item.id"
						@click="goPlay(item, index)"
						:style="{'background-image': 'url(' + item.upload_work_cover.path_text +')'  }">
						<view class="showMc"></view>
						<view class="showContent">
							<view class="title">{{item.title || '电影名称'}}</view>
							<view class="user">
								<view class="userName">
									<view class="userNameIcon" :style="{'background-image':'url(' + ((item.upload_avatar && item.upload_avatar.path_text) ? item.upload_avatar.path_text : '../../../static/home/default_user.png') + ')'}"></view>
									<view>
										{{item.user_info.nickname || '用户名'}}
									</view>
								</view>
								<view class="dzNum">
									{{item.like_count || 0}}
								</view>
							</view>
						</view>
					</view>
				</view>
				</scroll-view>
				<view class="noClass" v-if="videoList.length == 0">
					暂无视频
				</view>
			</swiper-item>
			<swiper-item>
				<scroll-view class="sView" :scroll-y="true" @scrolltolower="flash1" v-if="userList.length > 0">
				<view class="userSwiper">
					<view class="list" v-for="item in userList" :key="item.id">
						<view 
							class="headImg" 
							@click="goUserIndex(item)"
							:style="{'background-image':'url(' + ((item.upload_avatar && item.upload_avatar.path_text) ? item.upload_avatar.path_text : '../../../static/home/default_user.png') + ')'}">
						</view>
						<view class="info">
							<view class="name">{{item.nickname}}</view>
							<view class="fs">粉丝数:{{item.fans_count || 0}}</view>
							<view class="userId">用户ID:{{item.user_id}}</view>
						</view>
						<view class="gz wgz" v-if="!item.user_fans_to_user_id && item.user_id != userInfo.id" @click="goGz(item)">
							关注
						</view>
						<view class="gz ygz" v-if="item.user_fans_to_user_id && item.user_id != userInfo.id" @click="goQxgz(item)">
							已关注
						</view>
					</view>
				</view>
				</scroll-view>
				
				<view class="noClass" v-if="userList.length == 0">
					暂无用户
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {apiGetSearchZp, apiGetSearchUser, apiPostGz, apiPostQXGz} from '../../../../api/globSearch.js'
	export default {
		props: {
			test: {
				type: Boolean,
				default: true
			},
			flashSendUser: {
				type: Number,
				default: 0
			},
			searchValue: {
				type: String,
				default: ''
			}
		},
	    data() {
	        return {
				title: '',
				isResultShow: false,
				silderIndex: 0,
				pageIndexVideo: 1,
				pageSizeVideo: 10,
				pageIndexUser: 1,
				pageSizeUser: 10,
				videoList: [],
				tempVideoList: [],
				userList: [],
				tempUserList: [],
				// 个人信息
				userInfo: uni.getStorageSync('userinfo') || {}
	        }
	    },
		created() {
		},
	    methods: {
	        changeIndicatorDots(e) {
				this.silderIndex = e.detail.current
	        },
			flash0() {
				var self = this
				if (self.tempVideoList.length < self.pageSizeVideo) {
					return
				}
				self.pageIndexVideo++
				self.searchVideo({
					isFlash: 'isFlash',
					title: self.title
				})
			},
			
			flash1() {
				var self = this
				if (self.tempUserList.length < self.pageSizeUser) {
					return
				}
				self.pageIndexUser++
				self.searchUser({
					isFlash: 'isFlash',
					title: self.title
				})
			},
			// 对视频检索
			searchVideo(params) {
				var self = this
				if (!params.isFlash) {
					self.pageIndexVideo = 1
					self.pageSizeVideo = 10
				}
				apiGetSearchZp({
					title: params.title,
					page: self.pageIndexVideo,
					page_size: self.pageSizeVideo
				}).then(r => {
					self.tempVideoList = r.data.data
					if (params.isFlash) {
						self.videoList = self.videoList.concat(r.data.data)	
					}else {
						self.videoList = r.data.data
					}
				})
			},
			
			// 对用户检索
			searchUser(params) {
				var self = this
				if(!params.isFlash) {
					self.pageIndexUser = 1
					self.pageSizeUser = 10
				}
				apiGetSearchUser({
					nickname: params.title,
					page: self.pageIndexUser,
					page_size: self.pageSizeUser
				}).then(r => {
					self.tempUserList = r.data.data
					if (params.isFlash) {
						self.userList = self.userList.concat(r.data.data)
					} else {
						self.userList = r.data.data
					}
				})
			},
			
			// 点击播放视频
			goPlay(item, index) {
				let data = {
					videoType:'globSearch',
					page: parseInt((index + this.pageSizeVideo) / this.pageSizeVideo),
					pageSize: this.pageSizeVideo,
					videoIndex: (this.pageIndexVideo - 1) * this.pageSizeVideo + index,
					params: JSON.stringify({title: this.searchValue})
				}
				this.$navTo.togo('/pages/glob/video/index', data)
			},
			
			// 点击去关注
		    goGz(item) {
				var self = this
				apiPostGz({
					to_user_id: item.user_id
				}).then(r => {
					if (r.status == 0) {
						uni.showToast({
							title:'关注成功',
							icon:'success'
						})
						// 不刷新接口 做静态数据更新
						self.userList.map(function(v,i){
							if (v.user_id == item.user_id) {
								v.user_fans_to_user_id = {}
								v.user_fans_to_user_id.to_user_id = self.userInfo.id
							}
						})
						self.$store.dispatch('home/changeFocusStatus')
					}
				})
			},
			
			// 点击取消关注
			goQxgz(item) {
				var self = this
				apiPostQXGz({
					to_user_id: item.user_id
				}).then(r => {
					if (r.status == 0) {
						uni.showToast({
							title:'取消成功',
							icon:'success'
						})
						// 不刷新接口 做静态数据更新
						self.userList.map(function(v,i){
							if (v.user_id == item.user_id) {
								v.user_fans_to_user_id = null
							}
						})
						self.$store.dispatch('home/changeFocusStatus')
					}
				})
			},
			
			// 跳转用户中心
			goUserIndex(item) {
				var self = this
				// 判断下是不是自己
				if (item.user_id == self.userInfo.id) {
					// 跳转到自己的me
					uni.switchTab({
						url: '/pages/me/me'
					})
				}else {
					this.$navTo.togo('/pages/glob/user/index?userId=' + item.user_id)
				}
			},
			
			// tab切换
			getChnage(index) {
				var self = this
				self.silderIndex = index
			}
	    },
		mounted() {
		},
		watch: {
			'$store.getters.clickSearchSHow': {
				handler(val) {
					var self = this
					// 对视频或者用户进行检索
					if (val.value.type == 1) {
						// 用户
						self.isResultShow = true
						self.silderIndex = 1
						self.title = val.value.title
						self.searchUser({
							isFlash: null,
							title: val.value.title
						})
					} else {
						// 视频
						self.isResultShow = true
						self.silderIndex = 0
						self.title = val.value.title
						self.searchVideo({
							isFlash: null,
							title: val.value.title
						})
					}
				},
				deep: true
			},
			
			// 侧滑
			silderIndex() {
				var self = this
				if (self.silderIndex == 1) {
					// 用户
					self.searchUser({
						isFlash: null,
						title: self.title
					})
				} else {
					self.searchVideo({
						isFlash: null,
						title: self.title
					})
				}
			},
			
			test() {
				var self = this
				if(!self.test) {
					self.isResultShow = false
				}
			},
			
			// 下拉刷新
			flashSendUser() {
				var self = this
				if (self.silderIndex == 1) {
					self.pageIndexUser = 1
					self.pageSizeUser = 10
					self.searchUser({
						isFlash: null,
						title: self.title
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	.serachResult {
		width: 100%;
		height:83%;
		
		.tab {
			width: 94%;
			margin-left: 20rpx;
			height: 120rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			
			.tabUser {
				margin-left: 30rpx;
			}
			
			.tablist {
				font-size: 32rpx;
				color: #747384;
				height: 100%;
				line-height: 120rpx;
			}
			
			.active {
				color: #DDDBFB;
				background-image: url(../icon/xz.png);
				background-size: 32rpx 10rpx;
				background-repeat: no-repeat;
				background-position: center bottom;
			}
		}
		
		.swiper {
			width: 94%;
			height: 100%;
			margin-left: 20rpx;
			margin-top: 40rpx;
		}
		.videoSwiper {
			width: 100%;
			height: 100%;
			overflow-y: auto;
			margin-top: 40rpx;
			padding-bottom: 60rpx;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			.list {
				width: 47%;
				height: 600rpx;
				margin-bottom: 20rpx;
				background-size: cover;
				background-repeat: no-repeat;
				position: relative;
				
				.showMc {
					position: absolute;
					z-index: 99;
					background-color: #0C0B18;
					width: 100%;
					height: 184rpx;
					opacity: 0.3;
					bottom: 0;
				}
				.showContent {
					position: absolute;
					z-index: 100;
					width: 100%;
					height: 184rpx;
					bottom: 0;
					
					.title {
						font-size: 28rpx;
						width: 90%;
						height: 70rpx;
						margin-left: 5%;
						margin-top: 16rpx;
						overflow: hidden;
						line-height: 1.3;
					}
					
					.user {
						width: 90%;
						margin-left: 5%;
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin-top: 24rpx;
						font-size: 24rpx;
						height: 42rpx;
						.userName {
							display: flex;
							justify-content: space-between;
							align-items: center;
						}
						.userNameIcon {
							width: 42rpx;
							height: 42rpx;
							border-radius: 50%;
							background-image: url(../../../../static/home/default_user.png);
							background-size: 36rpx;
							background-position: left center;
							background-repeat: no-repeat;
							background-size: cover;
						}
						
						.dzNum {
							width: 42rpx;
							height: 42rpx;
							border-radius: 50%;
							background-size: 28rpx 25rpx;
							background-image: url(../icon/xh.png);
							background-position: 4px center;
							background-repeat: no-repeat;
							width: 30%;
							padding-left: 42rpx;
							line-height: 42rpx;
						}
					}
				}
			}
		}
		
		
		.userSwiper {
			width: 99%;
			height: 90%;
			overflow-y: auto;
			margin-top: 40rpx;
			padding-bottom: 60rpx;
			.list {
				width: 100%;
				height: 140rpx;
				display: flex;
				align-items: center;
				
				.headImg {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					background-size: cover;
					background-image: url(../../../../static/home/default_user.png);
					background-position: left center;
					background-repeat: no-repeat;
				}
				
				.info {
					width: 50%;
					margin-left: 32rpx;
					
					.name {
						color: #DDDBFB;
						font-size: 32rpx;
					}
					
					.fs, .userId {
						color: #747384;
						font-size: 24rpx;
					}
				}
				
				.gz {
					margin-left: 80rpx;
					width: 140rpx;
					height: 60rpx;
					border-radius: 4rpx;
					text-align: center;
					line-height: 60rpx;
					font-size: 24rpx;
				}
				
				.wgz {
					background-image: url(../../user/icon/gz.png);
					background-size: cover;
					font-size: 0;
				}
				
				.ygz {
					border-radius: 4rpx;
					font-size: 24rpx;
					background-color: #434254;
				}
			}
		}
		.noClass {
			margin-top: 150rpx;
			width: 100%;
			text-align: center;
		}
		
		.sView {
			height: 85vh;
			overflow-y: auto;
		}
	}
</style>
